package net.ibizsys.eclipse.studio;

import net.ibizsys.eclipse.studio.data.IBizUser;
import net.ibizsys.eclipse.studio.data.LocalDataManager;
import net.ibizsys.eclipse.studio.i18n.I18n;
import net.ibizsys.eclipse.studio.project.AppBuilder;
import net.ibizsys.eclipse.studio.util.StringKit;
import net.ibizsys.eclipse.studio.vc.SystemVCManager;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.equinox.security.storage.ISecurePreferences;
import org.eclipse.equinox.security.storage.SecurePreferencesFactory;
import org.eclipse.equinox.security.storage.StorageException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * 
 * @author <a href='http://www.weibo.com/xjtoo' target='_blank'>Enmaai</a>
 */
public class Activator extends AbstractUIPlugin implements IUserActivator
{
	public static final String PLUGIN_ID = "net-ibizsys-eclipse-studio";
	public static final String BUNDLE = "net.ibizsys.eclipse.studio";
	public static final String LoginNode = "net.ibizsys.eclipse.studio.data.login";

	/**
	 * 窗口宽度
	 */
	public static final int WindowWidth = 450;
	/**
	 * 窗口高度
	 */
	public static final int WindowHeight = 150;

	/**
	 * 主窗口高度
	 */
	public static final int MainWindowHeight = 500;

	/**
	 * 主窗口宽度
	 */
	public static final int MainWindowWidth = 700;

	/**
	 * 用户信息高度
	 */
	public static final int UserInfoHeight = 90;

	/**
	 * 输出框高度
	 */
	public static final int OutputInfoHeight = 180;

	/**
	 * 屏幕压缩率
	 */
	public static final double ScreenRate = 1366.0 / 1920;

	private static Activator plugin;
	private IBizUser currentUser = null;
	private boolean bLogined = false;

	public Activator()
	{
	}

	@Override
	public void start(BundleContext context) throws Exception
	{
		super.start(context);
		plugin = this;

		currentUser = LocalDataManager.manager().getUser();
		ISecurePreferences node = SecurePreferencesFactory.getDefault().node(LoginNode);
		bLogined = node.getBoolean("VALUE", false);

		try
		{
			SystemVCManager.build();
			AppBuilder.build();
		} catch (Exception ex)
		{
			MessageDialog.openError(getWorkbench().getDisplay().getShells()[0], I18n.Error, I18n.LoadBuilderError);
		}
	}

	@Override
	public void stop(BundleContext context) throws Exception
	{
		plugin = null;
		super.stop(context);
	}

	public static Activator getDefault()
	{
		return plugin;
	}

	public static double screenRate(Shell shell)
	{
		Rectangle rect = shell.getDisplay().getClientArea();

		if (rect.width > 1600)
		{
			return 1;
		} else
		{
			return ScreenRate;
		}
	}

	public String getRootPath()
	{
		return ResourcesPlugin.getWorkspace().getRoot().getLocation().toString();
	}

	@Override
	public boolean hasLogin()
	{
		return bLogined;
	}

	@Override
	public IBizUser curUser()
	{
		return currentUser;
	}

	@Override
	public void login(IBizUser user)
	{
		currentUser = user;

		try
		{
			bLogined = true;
			ISecurePreferences node = SecurePreferencesFactory.getDefault().node(LoginNode);
			node.putBoolean("VALUE", true, true);
		} catch (StorageException e)
		{
			e.printStackTrace();
		}
	}

	@Override
	public void logout()
	{
		bLogined = false;
		LocalDataManager.manager().removeUser();
		currentUser = LocalDataManager.manager().getUser();
		try
		{
			ISecurePreferences node = SecurePreferencesFactory.getDefault().node(LoginNode);
			node.putBoolean("VALUE", false, true);
		} catch (StorageException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 获取图片
	 * 
	 * @param path
	 * @return
	 */
	public static Image GetImage(String path)
	{
		if (StringKit.IsBlank(path))
		{
			return null;
		}
		return AbstractUIPlugin.imageDescriptorFromPlugin(Activator.getDefault().getBundle().getSymbolicName(), "$nl$/" + path).createImage();
	}

	/**
	 * 获取图片
	 * 
	 * @param path
	 * @return
	 */
	public static ImageDescriptor GetImageDesc(String path)
	{
		if (StringKit.IsBlank(path))
		{
			return null;
		}
		return AbstractUIPlugin.imageDescriptorFromPlugin(Activator.getDefault().getBundle().getSymbolicName(), "$nl$/" + path);
	}
}
